package com.sduept.nwld.mobile.rest;

import com.alibaba.fastjson.JSONObject;
import com.sduept.nwld.dataserver.manager.protection.RunlogManager;
import com.sduept.nwld.dataserver.model.daily.Runlog;
import com.sduept.nwld.mobile.manager.MobileUtil;
import com.sduept.utils.DateUtil;
import net.sf.json.JSONArray;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 客户端运行管理模块rest
 * 
 * @author zht
 *
 */
@RestController
@RequestMapping("/runManagerService/")
public class RunManagerService {
	@Autowired
	private RunlogManager rl;
	//SQL注入检测类
	@Inject
	private MobileUtil mobileUtil;
	
	/**
	 * 根据时间段获取调度日志信息
	 * 
	 * @return
	 * */

	@GetMapping("/getRunLogByDateAndType/{startDate}/{endDate}/{type}/{size}")
	public String getRunLogByDateAndType(@PathVariable("startDate") String startDate,@PathVariable("endDate") String endDate,@PathVariable("type") String type,@PathVariable("size") int size) {
		try {
			startDate = mobileUtil.SQLInjectCheck(String.valueOf(startDate));
			endDate = mobileUtil.SQLInjectCheck(String.valueOf(endDate));
			type = mobileUtil.SQLInjectCheck(String.valueOf(type));
			size = Integer.parseInt(mobileUtil.SQLInjectCheck(String.valueOf(size)));
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		List<Runlog> res=new ArrayList<Runlog>();
		if(!StringUtils.isEmpty(startDate)&&!StringUtils.isEmpty(endDate)){
			if("全部".equals(type)){
				res=rl.getRunlog(startDate+" 00:00:00", endDate+" 23:59:59",size);
			}else{
				res=rl.getRunLogByDateAndType(startDate+" 00:00:00", endDate+" 23:59:59",type,size);
			}
		}
		return JSONArray.fromObject(res).toString();
	}
	/**
	 * 根据时间段获取调度日志信息条目数量
	 * 
	 * @return
	 * */

	@GetMapping("/getRunLogCountByDateAndType/{startDate}/{endDate}/{type}")
	public String getRunLogCountByDateAndType(@PathVariable("startDate") String startDate,@PathVariable("endDate") String endDate,@PathVariable("type") String type) {
		try {
			startDate = mobileUtil.SQLInjectCheck(String.valueOf(startDate));
			endDate = mobileUtil.SQLInjectCheck(String.valueOf(endDate));
			type = mobileUtil.SQLInjectCheck(String.valueOf(type));
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		List<Runlog> res=new ArrayList<Runlog>();
		if(!StringUtils.isEmpty(startDate)&&!StringUtils.isEmpty(endDate)){
			if("全部".equals(type)){
				res=rl.getRunlog(startDate+" 00:00:00", endDate+" 23:59:59");
			}else{
				res=rl.getRunLogByDateAndType(startDate+" 00:00:00", endDate+" 23:59:59",type);
			}
		}
		return Integer.toString(res.size());
	}
	/**
	 * 根据时间段获取调度日志信息条目数量
	 * 
	 * @return
	 * */

	@GetMapping("/getRunLogCountByCondition/{param}")
	public String getRunLogCountByCondition(@PathVariable("param") String param) {
		JSONObject jso=JSONObject.parseObject(param);
		String startDate=jso.getString("startDate");
		String endDate=jso.getString("endDate");
		String type=jso.getString("type");
		String content=jso.getString("content");
		int size=jso.getIntValue("size");
		try {
			startDate =  mobileUtil.SQLInjectCheck(startDate);
			endDate =  mobileUtil.SQLInjectCheck(endDate);
			type =  mobileUtil.SQLInjectCheck(type);
			content =  mobileUtil.SQLInjectCheck(content);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		List<Runlog> res=new ArrayList<Runlog>();
		if(!StringUtils.isEmpty(startDate)&&!StringUtils.isEmpty(endDate)){
			Date sDate=DateUtil.getDateByString(startDate+" 00:00:00", DateUtil.pattern_ymdhms);
			Date eDate=DateUtil.getDateByString(endDate+" 23:59:59", DateUtil.pattern_ymdhms);
			res=rl.getRunlogByCondition(sDate,eDate,content,type);
		}
		return Integer.toString(res.size());
	}
	/**
	 * 根据条件获取调度日志信息
	 * 
	 * @return
	 * */

	@GetMapping("/getRunLogByCondition/{param}")
	public String getRunLogByDateAndType(@PathVariable("param") String param) {
		JSONObject jso=JSONObject.parseObject(param);
		String startDate=jso.getString("startDate");
		String endDate=jso.getString("endDate");
		String type=jso.getString("type");
		String content=jso.getString("content");
		int size=jso.getIntValue("size");
		try {
			startDate =  mobileUtil.SQLInjectCheck(startDate);
			endDate =  mobileUtil.SQLInjectCheck(endDate);
			type =  mobileUtil.SQLInjectCheck(type);
			content =  mobileUtil.SQLInjectCheck(content);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		List<Runlog> res=new ArrayList<Runlog>();
		if(!StringUtils.isEmpty(startDate)&&!StringUtils.isEmpty(endDate)){
			Date sDate=DateUtil.getDateByString(startDate+" 00:00:00", DateUtil.pattern_ymdhms);
			Date eDate=DateUtil.getDateByString(endDate+" 23:59:59", DateUtil.pattern_ymdhms);
			res=rl.getRunlogByCondition(sDate,eDate,content,type,size);
		}
		return JSONArray.fromObject(res).toString();
	}
	/**
	 * 获取所有的调度类型
	 * 
	 * @return
	 * */

	@GetMapping("/getAllRunLogTypes")
	public String getAllRunLogTypes() {
		return JSONArray.fromObject(rl.getRunlogTypes()).toString();
	}

}
